import { Component, OnInit } from '@angular/core';
import { DynamicDialogRef, DynamicDialogConfig } from 'primeng/primeng';
import { FormBuilder } from '@angular/forms';
import { MdlSnackbarService } from '@angular-mdl/core';
import { FreeflowService } from '../freeflow.service';

@Component({
    templateUrl: './ffact-filter.component.html',
    styleUrls: ['./ffact-filter.component.scss'],
    providers: []
})
export class FfActFilterComponent implements OnInit {
    actDefId: number;
    parts: any[];
    selectedPart: any;
    filterSql: string = '';
    cols: any[] = [
        { field: 'partId', header: '标识', style: { 'width': '80px', 'text-align': 'center' } },
        { field: 'partType', header: '类型', style: { 'width': '80px', 'text-align': 'center' } },
        { field: 'partName', header: '名称', style: { 'text-align': 'center' } },
        { field: 'filter', header: '过滤', style: { 'width': '80px', 'text-align': 'center' } }
    ];

    constructor(
        private fb: FormBuilder,
        private freeflowService: FreeflowService,
        private mdlSnackbarService: MdlSnackbarService,
        public ref: DynamicDialogRef,
        public config: DynamicDialogConfig) {
        this.actDefId = this.config.data.actDefId;
    }

    ngOnInit() {
        this.freeflowService.listProcActPartFilters(this.actDefId).subscribe(parts => {
            this.parts = parts;
            this.parts.forEach(ele => {
                if (ele['relationId'] > 1) {
                    ele['filter'] = true;
                }
            });
        })
    }

    /** 保存过滤SQL */
    save(){
        if(this.filterSql.trim().length > 0 && this.selectedPart){
            let params = {'filterSql':this.filterSql, 'actDefId': this.actDefId, 'partId':this.selectedPart.partId };
            this.freeflowService.saveUpdateActPartFilter(params).subscribe(data => {
                this.parts.forEach(ele => ele['partId'] ==  data['partId'] &&  (ele['filter'] = true));
                this.mdlSnackbarService.showToast("环节参与者过滤SQL保存成功");
            });
        }else{
            this.mdlSnackbarService.showToast("请选择参与者并填写过滤SQL信息");
        }
    }

    /** 选中事件 */
    onRowSelect($event){
        if($event.data.filter){
            this.freeflowService.loadUpdateActPartFilter(this.actDefId, this.selectedPart['partId']).subscribe(data => this.filterSql = data['filterSql']);
        }else{
            this.filterSql = '';
        }
    }

    /** 取消选中 */
    onRowUnselect($event){
        this.filterSql = '';
        this.selectedPart = null;
    }
    /** 关闭窗口 */
    cancel() {
        this.ref.close();
    }
    /** 移除过滤 */
    removeFilter(){
        this.freeflowService.removeFilter(this.actDefId, this.selectedPart.partId).subscribe(data => {
            this.selectedPart.filter = false;
            this.filterSql = '';
            this.mdlSnackbarService.showToast('移除流向过滤成功');
        });
    }
}
